package com.gzhryc.jdbc.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ModelCGLIBProxy extends ModelProxy implements MethodInterceptor{

	final Enhancer en;
	
	public ModelCGLIBProxy(Object obj) {
		super(obj);
		en = new Enhancer(); 
	}
	
	public Object intercept(Object obj, Method method, Object[] args,
			MethodProxy methodProxy) throws Throwable {
		before(method,args);
		return method.invoke(this.obj,args);
	}
	
	@SuppressWarnings("unchecked")
	public <T> T wrap() {
	    //进行代理     
	    en.setSuperclass(this.getObjectClass());     
	    en.setCallback(this);     
	    //生成代理实例     
	    return (T)en.create();
	}

}
